<!DOCTYPE html>
<html>
<head>
    {include file="../application/common/header.html" /}
    <script src="__STATIC__/js/echarts-3.8.4.js"></script>
</head>

<body>
    <h4 align="center" style="color:#060">学生历次考试成绩排名的升降图<br />（{$student["年级号"]}{$student["学号"]}{$student['姓名']}）</h4>
    <p align="center" id="choices"></p>
    <div id="studentRankChart" style="width:100%;height:550px; margin:0 auto 20px auto;"></div>
    
    <script type="text/javascript">
        var studentId = "{$student['ID']}";
        var grade = "{$student['年级号']}";
        var url = "/examScore/exam_chart_student";
        $(function(){
            loadReport();
        }) 
        function loadReport() {
            $.ajax({
               url: url + "/pageQuery.html",
               type: "POST",
               data: {
                  studentId:studentId,
                  grade: grade
               }
            }).done(function(data) {
                totalTermExamReport(data.rows);
            })
        }
        var examTemp = [];
        function totalTermExamReport(list) {
            //基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('studentRankChart'));
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(getOptionForList(list));
        }
        function getOptionForList(list) {
            var legendArray = getSubject(list);
            option = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: legendArray,
                    selected: (function() {
                        var array = {};
                        for(var i in legendArray) {
                            array[legendArray[i]] = parseInt(i) == 0 ? true : false;
                        }
                        return array;
                    }())
                },
                grid: {
                    hoverable:true,
                    bottom: '25%',
                    top: 100
                },
                xAxis: {
                    name: "考试",
                    type: 'category',
                    boundaryGap: false,
                    data: (function() {
                        var array = [];
                        for(var i = 0; i < list.length; i++) {
                            var examName = list[i].考试名称;
                            array.push(examName);
                            examTemp[examName] = list[i].考试ID;
                        }
                        return array;
                    }()),
                    axisLabel: {
                        interval: 0, 
                        rotate: 80
                    }
                },
                yAxis: {
                    type: 'value',
                    name: "名次",
                    minInterval: 1,
                    min: 1,
                    inverse: true,
                    nameLocation: 'start'
                },
                series: (function() {
                    var dataArray = [];
                    for(var i in legendArray) {
                        dataArray.push({
                            name: legendArray[i],
                            type:'line',
                            symbol: 'arrow',
                            symbolSize: 15,
                            data: []
                        });
                    }
                    for(var i = 0; i < list.length; i++) {
                        array = getSubjectScoreArrray(list[i]);
                        for(var j in dataArray) {
                            var exists = false; 
                            for(var k in array) {
                                if(array[k].subjectName == dataArray[j].name) {
                                    dataArray[j].data.push(array[k].ranking);
                                    exists = true;
                                }
                            }
                            if(!exists) {
                                dataArray[j].data.push("-");
                            }
                        }
                    }
                    return dataArray;
                })()
            };
            return option;
        }
        function getSubject(list) {
            var subject = [], map = new Map();
            for(var i in list) {
                var array = getSubjectScoreArrray(list[i]);
                for(var j in array) {
                    if(!map.get(array[j].subjectName) && ("总分级排名" != array[j].subjectName)){
                        map.set(array[j].subjectName, true);
                        subject.push(array[j].subjectName);
                    }
                }
            }
            subject.push("总分级排名");
            return subject;
        }
        function getSubjectScoreArrray(result) {
            var array = [];
            if(result.语文) array.push({subjectName: "语文名次",score: result.语文,ranking: result.语文名次});
            if(result.数学) array.push({subjectName: "数学名次",score: result.数学,ranking: result.数学名次});
            if(result.英语) array.push({subjectName: "英语名次",score: result.英语,ranking: result.englishRanking});
            if(result.物理) array.push({subjectName: "物理名次",score: result.物理,ranking: result.物理名次});
            if(result.化学) array.push({subjectName: "化学名次",score: result.chemistryScore,化学: result.化学名次});
            if(result.生物) array.push({subjectName: "生物名次",score: result.生物,ranking: result.生物名次});
            if(result.政治) array.push({subjectName: "政治名次",score: result.政治,ranking: result.政治名次});
            if(result.历史) array.push({subjectName: "历史名次",score: result.历史,ranking: result.历史名次});
            if(result.地理) array.push({subjectName: "地理名次",score: result.地理,ranking: result.地理名次});
            if(result.体育) array.push({subjectName: "体育名次",score: result.体育,ranking: result.体育名次});
            if(result.政史) array.push({subjectName: "政史名次",score: result.政史,ranking: result.政史名次});
            if(result.总分) array.push({subjectName: "总分级排名",score: result.总分,ranking: result.总分级排名,avgScore: ''});
            for(var i in array) {
                for(var j in result.scoreAvgList) {
                    if(result.scoreAvgList[j].subjectName == array[i].subjectName) {
                        array[i].avgScore = parseInt(result.scoreAvgList[j].avgScore);
                        break;
                    }
                }
            }
            return array;
        }
    </script>
</body>
</html>